package com.ruoyi.web.controller.tool;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class GenerateTxtFile
{
    public static void main(String[] args) {
        // 指定目录路径和文件名
        String directoryPath = "C:/example_directory";
        String fileName = "example.txt";

        // 创建目录（如果不存在）
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            boolean created = directory.mkdirs();
            if (created) {
                System.out.println("目录创建成功: " + directoryPath);
            } else {
                System.out.println("目录创建失败");
                return;
            }
        }

        // 创建文件并写入内容
        File file = new File(directory, fileName);
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for(int a=0; a<10;a++){

                writer.write("这是写入文件的内容"+a+"行");
                writer.newLine();
            }

            System.out.println("文件创建并写入成功: " + file.getAbsolutePath());
        } catch (IOException e) {
            System.out.println("文件操作出错: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
